<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\base\controller; //Demo插件英文名，改成你的插件英文就行了
use app\common\controller\Common;
use think\Request;
class Base extends Common
{
     function __construct()
    {
        $request = Request::instance();
        $token = $request->header('accesstoken'); 
        $projectId = $request->header('projectid');
        $this->checkSign($token,$projectId);
        // 验证
        $action = $request->action();
        $controller = $request->controller();
        switch ($action) {
            case 'select':
               $this->condition = $this->vdong('condition');
                // 验证
                $check = $this->validate($this->condition,$controller.'.select');
                // 验证失败 输出错误信息
                if(true !== $check) outFormat('',$check);
                break;
            case 'query':
                $this->condition = $this->vdong('condition');
                // 验证
                $check = $this->validate($this->condition, $controller.'.query');
                // 验证失败 输出错误信息
                if(true !== $check) outFormat('',$check);
                break;
            case 'insert':
                $this->postdata = $this->vdong('data');
                // 验证
                $check = $this->validate($this->postdata, $controller.'.insert');
                // 验证失败 输出错误信息
                if(true !== $check) outFormat('',$check);
                break;
            case 'update':
                $this->postdata = $this->vdong('condition');
                // 验证
                $check = $this->validate($this->condition, $controller.'.update');
                // 验证失败 输出错误信息
                if(true !== $check) outFormat('',$check);
                break;
            case 'delete':
                $this->condition = $this->vdong('condition');
                // 验证
                $check = $this->validate($this->condition, $controller.'.delete');
                // 验证失败 输出错误信息
                if(true !== $check) outFormat('',$check);
                break;
            default:
                break;
        }
    }
    //加密校验
    private function checkSign($token,$projectId){
      
      //1.获取请求头信息
      //2.获取商户数据并生成加密串
      $projectId = intval($projectId);
      // $info = Db::query('select * from cmf_user_project where id=?',[$projectId]);
      $info['salt'] = 'vdong';
      $sign = md5(md5($projectId.$info['salt'])); 
      //3.判断是否合法用户
      if($sign !== $token){
         outFormat('', 'access deny!');
      }
      
    }
    public function select()
    {
       $result =  $this->Model->getListByWhere();
       if(empty($result)){
        outFormats(0, '请求成功！','',0,0);
       }else{
        outFormats($result['data'], '请求成功！','',$result['current_page'],$result['total']);
       }
       
    }  
    public function query()
    {
       $result =  $this->Model->getOneByWhere();
       if(empty($result)){
        outFormat(0, '请求成功！');
       }else{
        outFormat($result, '请求成功！');
       }
       
    }  
    public function insert()
    {
       $result =  $this->Model->addData();
       outFormat($result, '新增成功');
    }  
    public function update()
    {
       $result =  $this->Model->saveData();
       outFormat($result, '更新成功');
    } 
    public function delete()
    {
       $result =  $this->Model->deleteOne();
       outFormat($result, '删除成功');
    }

    public function vdong($name) {
        $param = input();
        $array = $param[$name];
      //   $object=json_decode($param[$name]);
      // if (is_object($object)) {
      //   foreach ($object as $key => $value) {
      //     $array[$key] = $value;
      //   }
      // }
      // else {
      //   $array = $object;
      // }
      return $array;
    }
}
